//
//  FindMusicTableViewController.m
//  MoeFM
//
//  Created by miyang on 14-11-5.
//  Copyright (c) 2014年 StraightJet. All rights reserved.
//

#import "FindMusicTableViewController.h"

#import "UIViewController+ARTEmailSwipe.h"
#import "ARTEmailSwipe.h"

#define HOTURL @"http://moe.fm/explore?api=json&hot_radios=1&hot_musics=1&api_key=c6a6e84b505673382843850f8bed723d0545770eb"


@interface FindMusicTableViewController ()<UISearchBarDelegate>

@end

@implementation FindMusicTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        self.view.frame = CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height);
        _findMusic = [[FindMusic alloc] init];
        [self network];

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIButton *randomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [randomBtn setTitle:@"随意听" forState:UIControlStateNormal];
    randomBtn.frame = CGRectMake(20, 0, 139.5, 30);
//    randomBtn.backgroundColor = [UIColor blackColor];
    [randomBtn addTarget:self action:@selector(randomplay) forControlEvents:UIControlEventTouchUpInside];
    [randomBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    UIImageView *lineView = [[UIImageView alloc] initWithFrame:CGRectMake(159.5, 5, 1, 20)];
    [lineView setImage:[UIImage imageNamed:@"line.png"]];
    
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    searchBtn.frame = CGRectMake(160.5, 0, 139.5, 30);
//    searchBtn.backgroundColor = [UIColor blackColor];
    [searchBtn addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    [headView addSubview:randomBtn];
    [headView addSubview:searchBtn];
    [headView addSubview:lineView];
    
    self.tableView.tableHeaderView = headView;
    
    
//    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
//    search.searchBarStyle = UIBarStyleDefault;
//    search.placeholder = @"搜索商户名、地点    ";
//    search.delegate = self;
//    //去除搜索栏背景
//    [self searchBarClear:search];
//    _search = search;
//    search.autocorrectionType = UITextAutocorrectionTypeNo;//取消自动更正
//   // [searchView addSubview:search];
//    
//    self.navigationItem.titleView = _search;
//    self.navigationItem.titleView.backgroundColor = [UIColor clearColor];

    
}


-(void)viewWillAppear:(BOOL)animated
{
    
    static dispatch_once_t start;
    dispatch_once(&start, ^{
        [self.emailSwipeViewController openBottomView];
    });
    
}

- (void)randomplay
{
    NSString *urlStr = RANDOM_URL;
    [[PlayerViewController sharPlayer] playingWithURL:urlStr andAlbumTitle:@"随意听"];
}

- (void)search
{
    SearchTableViewController *search = [[SearchTableViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.radioarr.count == 0) {
        return 10;
    } else {
        if (section == 0) {
            return self.radioarr.count;
        }else {
            return self.musicarr.count;
        }
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"热门电台";
    }else {
        return @"热门专辑";
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"find"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"find"];
    }
    FindMusic *find = [[FindMusic alloc] init];
    if (indexPath.section == 0) {
        find = [_radioarr objectAtIndex:indexPath.row];
        cell.textLabel.text = find.wiki_title;
    }else {
        find = [_musicarr objectAtIndex:indexPath.row];
        cell.textLabel.text = find.wiki_title;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSelector:@selector(deselect) withObject:nil afterDelay:0.2f];
    if (indexPath.section == 0) {
        FindMusic *pass = [[FindMusic alloc] init];
        pass = [_radioarr objectAtIndex:indexPath.row];
        NSString *urlStr = [NSString stringWithFormat:RADIO_URL,pass.wiki_id];
        [[PlayerViewController sharPlayer] playingWithURL:urlStr andAlbumTitle:@"热门电台"];
        
        
    } else {
        FindMusic *pass = [[FindMusic alloc] init];
        pass = [_musicarr objectAtIndex:indexPath.row];
        NSString *urlStr = [NSString stringWithFormat:MUSIC_URL,pass.wiki_id];
        [[PlayerViewController sharPlayer] playingWithURL:urlStr andAlbumTitle:@"热门专辑"];
    }

}
- (void)network
{
    AFHTTPRequestOperationManager *music = [AFHTTPRequestOperationManager manager];
    [music GET:HOTURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [[NSDictionary alloc] init];
        dic = [responseObject objectForKey:@"response"];
        NSArray *hot_musics = [dic objectForKey:@"hot_musics"];
        NSArray *hot_radios = [dic objectForKey:@"hot_radios"];
        
        NSMutableArray *musicarr = [[NSMutableArray alloc] init];
        NSMutableArray *radioarr = [[NSMutableArray alloc] init];
        
        for (NSDictionary *musicDic in hot_musics) {
            FindMusic *find = [[FindMusic alloc] init];
            
            find.wiki_name = [musicDic objectForKey:@"wiki_name"];
            find.wiki_title = [musicDic objectForKey:@"wiki_title"];
            find.wiki_meta = [musicDic objectForKey:@"wiki_meta"];
            find.wiki_cover = [musicDic objectForKey:@"wiki_cover"];
            find.wiki_id = [musicDic objectForKey:@"wiki_id"];
            [musicarr addObject:find];
        }
        self.musicarr = musicarr;
        
        for (NSDictionary *radioDic in hot_radios) {
            FindMusic *find = [[FindMusic alloc] init];
            find.wiki_name = [radioDic objectForKey:@"wiki_name"];
            find.wiki_title = [radioDic objectForKey:@"wiki_title"];
            find.wiki_meta = [radioDic objectForKey:@"wiki_meta"];
            find.wiki_cover = [radioDic objectForKey:@"wiki_cover"];
            find.wiki_id = [radioDic objectForKey:@"wiki_id"];
            [radioarr addObject:find];
        }
        self.radioarr = radioarr;
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD showError:@"网络错误"];
    }];
}

- (void)deselect
{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
